#! /usr/bin/env python
#coding=utf-8

from get_month_quarter import *
from get_tasks_develop_report_detail import *

db = get_db_connection()

meta = MetaData(db)
db.metadata.reflect()

def run():

    first_day,last_day,month_view = get_month()
    #print last_day,first_day

    req_resource = get_model('resource')
    req_his = get_model('resourcehis')
    obj_resource =  req_resource.filter()
    int_last_day = int(str(last_day)[0:4])*100 + int(str(last_day)[5:7])
    req_his.remove(and_(req_his.c.yearmon == int_last_day))
    #删除上上月置删除标志的记录
    req_resource.remove(and_(req_resource.c.delete_flag==True,req_resource.c.delete_date<first_day))
    
    for row in obj_resource:
        
#        print row
        row = row.to_dict(['user','kfzx','type','system','role','workload','delete_flag','delete_date','is_selfdev'])
#        print row
#        month = str(last_day)[5:7]
#        year = str(last_day)[0:4]
        
        row['yearmon'] = int(str(last_day)[0:4])*100 + int(str(last_day)[5:7])
#        row['yearmon'] = last_day
#        print row
        row['workday'] = 0
        row['worktime'] = 0
        
        r = req_his(**row)
        r.save()
        
    





if __name__ == '__main__':
    try:
        db.begin()
        run()
        db.commit()
        log.info("finish")
    except Exception, e:
        db.rollback()
        log.exception(e)
        log.error('error')
        raise
    
